package cn.wen.eeayjoke.ui;

import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.wen.base.adapter.recyclerview.QuickRecyclerAdapter;
import cn.wen.base.adapter.recyclerview.QuickRecyclerViewHolder;
import cn.wen.base.decoration.LinearItemDecoration;
import cn.wen.base.view.SwipeMenuLayout;
import cn.wen.core.refresh.RefreshRecyclerView;
import cn.wen.core.refresh.XRecyclerView;
import cn.wen.core.titlebar.DefTitleBar;
import cn.wen.core.ui.AppActivity;
import cn.wen.eeayjoke.R;

/**
 * 下拉刷新和加载更多的View
 * 1. 每个APP下拉刷新的头和加载更多都不一样。自己去稍作定制即可
 * 2. 自定义下拉刷新的头：DefaultRefreshCreator到这个类改，在lib-core->>refresh--creator里面
 * 3. 自定义加载更多：DefaultLoadCreator到这个类改，在lib-core->>refresh--creator里面
 */
public class RefreshTestActivity extends AppActivity {
    private XRecyclerView mRecyclerView;
    private QuickRecyclerAdapter<String> mAdapter;
    private List<String> data;

    @Override
    public Object getContentLayout() {
        return R.layout.activity_refresh;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        data = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            data.add("i-->>" + i);
        }
    }

    @Override
    public void initView(View contentView) {
        mRecyclerView = findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new LinearItemDecoration(this));
        mRecyclerView.setAdapter(mAdapter = new QuickRecyclerAdapter<String>(getAppActivity(), null, R.layout.item_refresh_test) {
            @Override
            protected void convert(final QuickRecyclerViewHolder holder, final String item, final int position) {
                holder.setText(R.id.tv, item);
                holder.setOnClickListener(R.id.tv, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getAppActivity(), "" + item, Toast.LENGTH_SHORT).show();
                    }
                });
                holder.setOnClickListener(R.id.tv_delete, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mAdapter.remove(item);
                        //在列表里，点击侧滑菜单上的选项时，如果想让侧划菜单同时关闭，调用这句话
                        if (holder.getView(R.id.delete_swipe) instanceof SwipeMenuLayout) {
                            ((SwipeMenuLayout) holder.getView(R.id.delete_swipe)).quickClose();
                        }
                    }
                });
            }
        });
        // 加载更多
        mRecyclerView.setOnLoadMoreListener(new XRecyclerView.OnLoadMoreListener() {
            @Override
            public void onLoading() {
                loadMore();
            }
        });
        // 下拉刷新
        mRecyclerView.setOnRefreshListener(new RefreshRecyclerView.OnRefreshListener() {
            @Override
            public void onRefreshing() {
                refresh();
            }
        });
        // 设置自动加载更多
        mRecyclerView.setAutoLoadMore(true);
        // 设置设置自动下拉的时间 默认1秒
        mRecyclerView.setAutoTime(500);
        // 自动下拉刷新
        mRecyclerView.autoRefresh();
    }

    private void loadMore() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(1500);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter.add("load-->>");
                        mRecyclerView.stopLoad();
                    }
                });
            }
        }).start();
    }

    private void refresh() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(1500);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter.clear();
                        mAdapter.addAll(data);
                        mRecyclerView.setEnableLoadMore(true);
                        mRecyclerView.stopRefresh();
                    }
                });
            }
        }).start();

    }


    @Override
    protected void initTitle(DefTitleBar titleBar) {
        titleBar.setTitle("刷新测试");
    }


}
